import time
import sys
import json
import io
import os

# 设置sys.stdout的编码为UTF-8
sys.stdout.reconfigure(encoding='utf-8')

from DrissionPage._pages.chromium_tab import ChromiumTab


def upload_dir_images(tab: ChromiumTab,image_dir,uploadImagePaths):
    openUploadImagePath = uploadImagePaths['openUploadImagePath']
    selectUploadImagePath = uploadImagePaths['selectUploadImagePath']
    closeUploadImagePath = uploadImagePaths['closeUploadImagePath']

    containerPath = uploadImagePaths['containerPath']
    container_span = tab.ele('xpath:' + containerPath)

    # 所有已经上传的图片 文件名称
    upload_image_names = []
    if container_span:
        image_name_spans = container_span.eles('tag:span@@class^' + 'PicturesShow_tooltipText_')
        if image_name_spans:
            for image_name in image_name_spans:
                upload_image_names.append(image_name.text)

    # 查找没有上传的文件
    file_list = []
    for filename in os.listdir(image_dir):
        file_path = os.path.join(image_dir, filename)
        if os.path.isfile(file_path):
            need_upload = True
            for image_name in upload_image_names:
                if image_name == filename:
                    need_upload = False
            if need_upload:
                file_list.append(file_path)

    if len(file_list) == 0:
        return
    # 设置要上传的文件路径
    # tab.set.upload_files([os.path.join(image_dir, i) for i in os.listdir(image_dir)])
    tab.set.upload_files(file_list)
    # 点击触发文件选择框按钮
    tab.ele('xpath:' + openUploadImagePath).click()
    time.sleep(2)
    tab.ele('xpath:' + selectUploadImagePath).click()
    time.sleep(2)
    # 等待路径填入
    tab.wait.upload_paths_inputted()
    time.sleep(5)
    # 关闭
    tab.ele('xpath:' + closeUploadImagePath).click()


def check_and_create_dir(tab: ChromiumTab, uploadImagePaths, dir_name):
    containerPath = uploadImagePaths['containerPath']
    container_span = tab.ele('xpath:' + containerPath)
    # 目录下有文件或文件夹
    if container_span:
        has_dir = db_click_dir(tab, container_span, dir_name)
        if not has_dir:
            create_dir(tab, uploadImagePaths, dir_name)

    else:  # 文件夹为空, 需要创建目录
        create_dir(tab, uploadImagePaths, dir_name)


# 没有就创建目录
def db_click_dir(tab: ChromiumTab, container_span, dir_name):
    top_dir_element = container_span.ele('tag:div@@text()=' + dir_name)
    if top_dir_element:
        # 双击 第一层 目录
        top_dir_img = top_dir_element.ele('tag:img')
        tab.actions.db_click(top_dir_img)
        time.sleep(2)
        return True
    return False
# 没有就创建目录
def create_dir(tab: ChromiumTab, uploadImagePaths, dir_name):
        containerPath = uploadImagePaths['containerPath']
        containerDivId = uploadImagePaths['containerDivId']
        worbenchContainerDivId = uploadImagePaths['worbenchContainerDivId']

        icestarkNode = tab.ele('#' +containerDivId)
        worbench_container = tab.ele('#' + worbenchContainerDivId)
        # 新建文件夹 按钮
        dir_element = icestarkNode.ele('tag:button@@text()=' + '新建文件夹')
        tab.actions.click(dir_element)
        time.sleep(2)
        new_dir_open_div = worbench_container.ele('tag:div@@class=next-dialog-header@@text()=' + '新建文件夹')
        new_dir_open_top_div = new_dir_open_div.parent()

        new_dir_name_element = new_dir_open_top_div.ele('tag:span@@text()=' + '新文件夹名称')
        # 文件夹名称
        input_element = new_dir_name_element.next().ele('tag:input')
        tab.actions.click(input_element)
        time.sleep(1)
        input_element.input(dir_name)
        time.sleep(1)

        # 点击 确定 按钮
        input_element = new_dir_open_top_div.ele('tag:button@@text()=' + '确定')
        tab.actions.click(input_element)
        time.sleep(2)

        # 重新获取列表
        container_span = tab.ele('xpath:' + containerPath)
        db_click_dir(tab, container_span, dir_name)


